﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mesoBoard.Data.Repositories
{
    public class AttachmentRepository : IAttachmentRepository
    {
        private mbEntities db;

        public AttachmentRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public IEnumerable<Attachment> GetPostAttachments(int PostID)
        {
            return db.Attachments.Where(x => x.PostID == PostID);
        }

        public Attachment GetAttachment(int AttachmentID)
        {
            return db.Attachments.FirstOrDefault(x => x.AttachmentID == AttachmentID);
        }

        public void CreateAttachement(Attachment TheAttach)
        {
            db.Attachments.AddObject(TheAttach);
            db.SaveChanges();
        }

        public void DeleteAttachment(int AttachmentID)
        {
            db.Attachments.DeleteObject(db.Attachments.First(x => x.AttachmentID == AttachmentID));
            db.SaveChanges();
        }


        public void UpdateAttachment(Attachment attachment)
        {
            if (attachment.EntityKey == null)
                attachment.EntityKey = ((Attachment)db.GetObjectByKey(new System.Data.EntityKey("mbEntities.Attachments", "AttachmentID", attachment.AttachmentID))).EntityKey;

            db.Attachments.ApplyCurrentValues(attachment);
            db.SaveChanges();
        }
    }
}
